package com.campus.config;


import com.campus.constant.PublicPaths;
import com.campus.fitter.UserFitter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfig {
    @Autowired
    private UserFitter userFitter;
    @Autowired
    private AuthenticationEntryPoint authenticationHandle;
    @Autowired
    private AccessDeniedHandler accessDeniedHandler;


    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder (){
        return new BCryptPasswordEncoder();
    }
    @Bean
    public AuthenticationManager authenticationProvider(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();

    }
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .cors(cors -> cors.configurationSource(request -> {
                    var source = new org.springframework.web.cors.CorsConfiguration();
                    source.setAllowCredentials(true);
                    source.addAllowedOriginPattern("*"); // 允许所有来源
                    source.addAllowedHeader("*"); // 允许所有头
                    source.addAllowedMethod("*"); // 允许所有请求方法
                    return source;
                }))
                .csrf(AbstractHttpConfigurer::disable) // 禁用CSRF保护
                .authorizeHttpRequests(authorize -> authorize
                        .requestMatchers(PublicPaths.PUBLIC_PATHS).permitAll()
                        .anyRequest().authenticated() // 其他请求需要认证
                )
                .exceptionHandling(exceptionHandling -> exceptionHandling
                                .authenticationEntryPoint(authenticationHandle) // 注册自定义异常处理
                                .accessDeniedHandler(accessDeniedHandler)
                )
                .addFilterBefore(userFitter,UsernamePasswordAuthenticationFilter.class)
                .formLogin(form -> form
                        .loginPage("/login") // 自定义登录页面
                        .permitAll()
                );
        return http.build();
    }


}
